package com.huaxin.device.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.huaxin.device.models.SwitchCabinet;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

@Mapper
public interface SwitchCabinetMapper extends BaseMapper<SwitchCabinet> {
    @Select("SELECT COUNT(*) > 0 FROM switch_cabinet WHERE room_id = #{roomId}")
    boolean existsByRoomId(String roomId);

    @Select("SELECT COUNT(*) > 0 FROM switch_cabinet WHERE type = #{type}")
    boolean existsByType(String type);

    /**
     * 根据配电房ID列表，统计每个配电房关联的开关柜数量（去重）
     *
     * @param roomIds 配电房ID列表
     * @return List<Map < String, Object>>，包含 room_id 和 count
     */
    @Select({
            "<script>",
            "SELECT room_id, COUNT(*) AS count FROM switch_cabinet",
            "WHERE is_deleted = 0 AND room_id IN",
            "<foreach item='id' collection='roomIds' open='(' separator=',' close=')'>#{id}</foreach>",
            "GROUP BY room_id",
            "</script>"
    })
    @Results({
            @Result(column = "room_id", property = "room_id"),
            @Result(column = "count", property = "count")
    })
    List<Map<String, Object>> countCabinetsByRoomIds(@Param("roomIds") List<String> roomIds);

}